﻿Imports lbGlobalcom
Imports Oracle.DataAccess.Client
Public Class fOrdenCompra
    Dim secuencia As Integer
    Dim pCodigoProducto As String
    Dim daTipodoc As lbGlobalcom.dsGlobalTableAdapters.GLO_TIPODOCUMENTOTableAdapter
    Dim daOrdenCompraEncabeza As lbGlobalcom.dsComprasTableAdapters.COM_ENCABEZADOORDENCOMPRATableAdapter
    Dim daOrdenCompraDtalle As lbGlobalcom.dsComprasTableAdapters.COM_DETALLEORDENCOMPRATableAdapter
    Dim daConsecutivo As lbGlobalcom.dsGlobalTableAdapters.GLO_CONSECUTIVOTableAdapter
    Dim daDatTab As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Dim daProductos As lbGlobalcom.dsProductosTableAdapters.PRO_PRODUCTOTableAdapter
    Dim cOrdCom As lbGlobalcom.cEncabezadoOrdenCompra
    Dim cListaPrecio As lbGlobalcom.cDetalleListaPrecios
    Dim iTipoDoc As cTipoDocumento
    Private lEliminaDoc As Boolean = False
    Private mensajeValidacion As String = ""
    Private Sub COM_ENCABEZADOORDENCOMPRABindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles COM_ENCABEZADOORDENCOMPRABindingNavigatorSaveItem.Click
        Try
            Dim cVal As New cValidaciones(DsCompras)
            COM_ENCABEZADOORDENCOMPRABindingSource.EndEdit()
            mensajeValidacion = cVal.ValidaOrdenCompra(iTipoDoc.DocEs)
            If mensajeValidacion = "" Then
                SalvaCambios(True)
            Else
                MessageBox.Show(mensajeValidacion, "validaciones", MessageBoxButtons.OK)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub fOrdenCompra_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If DsCompras.COM_DETALLEORDENCOMPRA.GetChanges IsNot Nothing Or DsCompras.COM_ENCABEZADOORDENCOMPRA.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desae salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaCambios(False)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
   
    Private Sub fOrdenCompra_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Try
            If e.Control Then
                Select Case e.KeyCode
                    Case Keys.B
                        Dim fLOrdCom As New fListaOrdenCompra
                        If fLOrdCom.ShowDialog = Windows.Forms.DialogResult.OK Then
                            DsCompras.COM_DETALLEORDENCOMPRA.Rows.Clear()
                            DsCompras.COM_ENCABEZADOORDENCOMPRA.Rows.Clear()
                            daOrdenCompraEncabeza.FillByTipoDocnumero(DsCompras.COM_ENCABEZADOORDENCOMPRA, fLOrdCom.Ptipodoc, fLOrdCom.Pnumero)
                            daOrdenCompraDtalle.FillByTipoDocNumero(DsCompras.COM_DETALLEORDENCOMPRA, fLOrdCom.Ptipodoc, fLOrdCom.Pnumero)
                            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
                        End If
                    Case Keys.S
                        cOrdCom = New lbGlobalcom.cEncabezadoOrdenCompra(TIPODOCTextBox.Text, NUMEROTextBox.Text, "COM")
                        'COM_ENCABEZADOORDENCOMPRABindingSource.EndEdit()
                        If cOrdCom.validarNumero(NUMEROTextBox.Text) = 0 Then
                            NUMEROTextBox.Text = cOrdCom.consecutivoOrdenCompra()
                        End If
                        daOrdenCompraEncabeza.Update(DsCompras.COM_ENCABEZADOORDENCOMPRA)
                        COM_DETALLEORDENCOMPRABindingSource.EndEdit()
                        daOrdenCompraDtalle.Update(DsCompras.COM_DETALLEORDENCOMPRA)
                        MessageBox.Show("Se salvarón registros con exito", "Salva Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fOrdenCompra_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        daConsecutivo = New lbGlobalcom.dsGlobalTableAdapters.GLO_CONSECUTIVOTableAdapter
        daOrdenCompraEncabeza = New lbGlobalcom.dsComprasTableAdapters.COM_ENCABEZADOORDENCOMPRATableAdapter
        daOrdenCompraDtalle = New lbGlobalcom.dsComprasTableAdapters.COM_DETALLEORDENCOMPRATableAdapter
        daDatTab = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        daDatTab.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "es")
        lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
        daProductos = New lbGlobalcom.dsProductosTableAdapters.PRO_PRODUCTOTableAdapter
        daTipodoc = New lbGlobalcom.dsGlobalTableAdapters.GLO_TIPODOCUMENTOTableAdapter
        DsCompras.COM_ENCABEZADOORDENCOMPRA.FECHAColumn.DefaultValue = Date.Today
        DsCompras.COM_ENCABEZADOORDENCOMPRA.FECHAPAGOColumn.DefaultValue = Date.Today
        DsCompras.COM_ENCABEZADOORDENCOMPRA.FECHARECEPCIONColumn.DefaultValue = Date.Today
        DsCompras.COM_ENCABEZADOORDENCOMPRA.ESTADOORDENColumn.DefaultValue = 1
        cGeneral.BloquearOrdenDatagrid(COM_DETALLEORDENCOMPRADataGridView)
    End Sub

    Private Sub btnBuscaTipoDocumento_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTipoDocumento.Click
        Dim fLDocumento As New fListaTipoDocs
        fLDocumento.SubClase = "OC"
        If fLDocumento.ShowDialog = Windows.Forms.DialogResult.OK Then
            TIPODOCTextBox.Text = fLDocumento.idTipoDoc
            iTipoDoc = New cTipoDocumento(fLDocumento.idTipoDoc)
            NUMEROTextBox.Text = lbGlobalcom.Consec.GetNumeroActual(fLDocumento.idTipoDoc, iTipoDoc.DocEs)
            IDCCOSTOTextBox.Text = iTipoDoc.idCcosto
            BODEGAPARATextBox.Text = iTipoDoc.BodegaPara
            SUCURSALGLOBALCOMTextBox.Text = CustomPrincipal.GetSede
            COM_ENCABEZADOORDENCOMPRABindingSource.EndEdit()
        End If
    End Sub

    Private Sub TIPODOCTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TIPODOCTextBox.TextChanged
        cOrdCom = New lbGlobalcom.cEncabezadoOrdenCompra(TIPODOCTextBox.Text, "0", "COM")
        Try
            If TIPODOCTextBox.Text <> "" Then
                NUMEROTextBox.Text = cOrdCom.getConsecutivoOrdenCompra()
                'NUMEROTextBox.Text = daConsecutivo.GetNumeroActual("OC")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaBodega_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaBodega.Click
        Dim fLBodegas As New fListaBodegas
        If fLBodegas.ShowDialog = Windows.Forms.DialogResult.OK Then
            BODEGAPARATextBox.Text = fLBodegas.Bodega
        End If
    End Sub

    Private Sub btnBuscaMinRecarga_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaMinRecarga.Click
        Dim flMinRecarga As New fListaMinRecargaPas
        If flMinRecarga.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDMINPADRETextBox.Text = flMinRecarga.MinRecarga
        End If
    End Sub

    Private Sub btnBuscaSede_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaSede.Click
        Dim flSedes As New fListaSedes
        If flSedes.ShowDialog = Windows.Forms.DialogResult.OK Then
            SUCURSALGLOBALCOMTextBox.Text = flSedes.Sede
        End If
    End Sub

    Private Sub btnBuscaProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaProveedor.Click
        Dim flProveedor As New fBuscaTercero
        flProveedor.TipoTercero = "Proveedor"
        If flProveedor.ShowDialog = Windows.Forms.DialogResult.OK Then

            IDPROVEEDORTextBox.Text = flProveedor.midTercero
        End If
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            'secuencia = 1
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            If DsCompras.COM_DETALLEORDENCOMPRA.GetChanges IsNot Nothing Or DsCompras.COM_ENCABEZADOORDENCOMPRA.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desae salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaCambios(False)
                End If
            End If
            COM_ENCABEZADOORDENCOMPRABindingSource.CancelEdit()
            DsCompras.COM_DETALLEORDENCOMPRA.Rows.Clear()
            DsCompras.COM_DETALLEORDENCOMPRA.Rows.Clear()
            ESTADOORDENListBox.SelectedValue = 1
            TIPODOCTextBox.Enabled = True
            NUMEROTextBox.Enabled = True
            btnBuscaTipoDocumento.Enabled = True
            COM_ENCABEZADOORDENCOMPRABindingSource.AddNew()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub addProductos(ByVal idProducto As String)
        Try
            COM_DETALLEORDENCOMPRADataGridView.CurrentRow.Cells("IDPRODUCTO").Value = idProducto
            Dim IdLista As String = daTipodoc.GetIdLista(TIPODOCTextBox.Text)
            Dim rowProducto As dsProductos.PRO_PRODUCTORow = daProductos.GetDataByIdProducto(idProducto).Rows(0)
            cListaPrecio = New lbGlobalcom.cDetalleListaPrecios(IdLista, idProducto)
            If rowProducto IsNot Nothing Then
                COM_DETALLEORDENCOMPRADataGridView.CurrentRow.Cells("UNIDAD").Value = rowProducto.UNIDAD
                If Not (rowProducto.IsREFERENCIANull) Then
                    COM_DETALLEORDENCOMPRADataGridView.CurrentRow.Cells("IDPRODUCTOPROVEEDOR").Value = rowProducto.REFERENCIA
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub SalvaCambios(ByVal Msj As Boolean)
        'If NUMEROTextBox.Text IsNot Nothing AndAlso NUMEROTextBox.Text <> "" Then
        '    cOrdCom = New lbGlobalcom.cEncabezadoOrdenCompra(TIPODOCTextBox.Text, NUMEROTextBox.Text, "COM")
        Try
            If Msj Then _
               Validate()
            TIPODOCTextBox.Enabled = False
            NUMEROTextBox.Enabled = False
            btnBuscaTipoDocumento.Enabled = False
            COM_ENCABEZADOORDENCOMPRABindingSource.EndEdit()
            COM_DETALLEORDENCOMPRABindingSource.EndEdit()
            If lEliminaDoc = True Then
                daOrdenCompraDtalle.Update(DsCompras.COM_DETALLEORDENCOMPRA)
                daOrdenCompraEncabeza.Update(DsCompras.COM_ENCABEZADOORDENCOMPRA)
            Else
                daOrdenCompraEncabeza.Update(DsCompras.COM_ENCABEZADOORDENCOMPRA)
                daOrdenCompraDtalle.Update(DsCompras.COM_DETALLEORDENCOMPRA)
            End If
            If Msj Then _
                MessageBox.Show("Se salvarón cambios con exito", "Salvar Cambios", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
        'End If
    End Sub


    Private Sub btnBuscaOrdenCompra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaOrdenCompra.Click
        Dim fLOrdCom As New fListaOrdenCompra
        Try
            fLOrdCom.BuscaDesde = 0
            If fLOrdCom.ShowDialog = Windows.Forms.DialogResult.OK Then
                If DsCompras.COM_DETALLEORDENCOMPRA.GetChanges IsNot Nothing Or DsCompras.COM_ENCABEZADOORDENCOMPRA.GetChanges IsNot Nothing Then
                    If MessageBox.Show("Hay cambios pendientes por salvar, desae salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                        SalvaCambios(False)
                    End If
                End If
                DsCompras.COM_DETALLEORDENCOMPRA.Rows.Clear()
                DsCompras.COM_ENCABEZADOORDENCOMPRA.Rows.Clear()
                daOrdenCompraEncabeza.FillByTipoDocnumero(DsCompras.COM_ENCABEZADOORDENCOMPRA, fLOrdCom.Ptipodoc, fLOrdCom.Pnumero)
                daOrdenCompraDtalle.FillByTipoDocNumero(DsCompras.COM_DETALLEORDENCOMPRA, fLOrdCom.Ptipodoc, fLOrdCom.Pnumero)
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
                iTipoDoc = New cTipoDocumento(fLOrdCom.Ptipodoc)
                TIPODOCTextBox.Enabled = False
                NUMEROTextBox.Enabled = False
                btnBuscaTipoDocumento.Enabled = False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub




    Private Sub btnBuscaCcosto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCcosto.Click
        Dim flCcosto As New fListaCCostos
        Try
            If flCcosto.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCCOSTOTextBox.Text = flCcosto.CCosto
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaFormaPago_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaFormaPago.Click
        Dim flFormaPago As New fListaFormasPago
        Try
            If flFormaPago.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDFORMAPAGOTextBox.Text = flFormaPago.FP
                FECHAPAGODateTimePicker.Value = FECHARECEPCIONDateTimePicker.Value.AddDays(flFormaPago.Dias)
                'flFormaPago.
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaCentroSumi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCentroSumi.Click
        Try
            Dim fLCentro As New fListaCentroSumi
            If fLCentro.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCENTROSUMITextBox.Text = fLCentro.mIdCentroSumi
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub COM_DETALLEORDENCOMPRADataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles COM_DETALLEORDENCOMPRADataGridView.CellBeginEdit
        Try
            COM_DETALLEORDENCOMPRADataGridView.Rows(e.RowIndex).Cells("LINEA").Value = cGeneral.GetNumeroLinea(COM_DETALLEORDENCOMPRADataGridView, e.RowIndex)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub COM_DETALLEORDENCOMPRADataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles COM_DETALLEORDENCOMPRADataGridView.CellMouseDoubleClick
        Try
            Select Case e.ColumnIndex
                Case 3
                    If TIPODOCTextBox.Text <> "" And NUMEROTextBox.Text <> "" Then
                        If COM_DETALLEORDENCOMPRADataGridView.CurrentCellAddress.X = 3 Then
                            Dim fLProd As New fListaProductos
                            fLProd.ListaPrecios = daTipodoc.GetIdLista(TIPODOCTextBox.Text)
                            If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                                COM_DETALLEORDENCOMPRADataGridView.BeginEdit(True)
                                COM_DETALLEORDENCOMPRADataGridView.Rows(COM_DETALLEORDENCOMPRADataGridView.CurrentRow.Index).Cells(3).Value = fLProd.idArticulo
                                COM_DETALLEORDENCOMPRADataGridView.EndEdit()
                                Me.txtDescripProducto.Text = daProductos.GetNombreProducto(fLProd.idArticulo)
                                pCodigoProducto = fLProd.idArticulo
                                addProductos(pCodigoProducto)
                                ActualizaDatosProd(COM_DETALLEORDENCOMPRADataGridView.CurrentCellAddress.Y)
                            End If
                        End If
                    End If
            End Select
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub ActualizaDatosProd(ByVal Fila As Integer)
        Try
            If COM_DETALLEORDENCOMPRADataGridView.CurrentRow IsNot Nothing Then
                Dim IdLista As String = daTipodoc.GetIdLista(TIPODOCTextBox.Text)
                cListaPrecio = New lbGlobalcom.cDetalleListaPrecios(IdLista, COM_DETALLEORDENCOMPRADataGridView.Rows(Fila).Cells("IDPRODUCTO").Value)
                COM_DETALLEORDENCOMPRADataGridView.Rows(Fila).Cells(6).Value = cListaPrecio.PrecioCosto
                COM_DETALLEORDENCOMPRADataGridView.Rows(Fila).Cells(7).Value = cListaPrecio.PrecioCosto * cListaPrecio.iProducto.iTipoIva.PorIva
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub COM_DETALLEORDENCOMPRADataGridView_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles COM_DETALLEORDENCOMPRADataGridView.CellValidated
        Try
            If COM_DETALLEORDENCOMPRADataGridView.IsCurrentCellInEditMode Then
                Select Case COM_DETALLEORDENCOMPRADataGridView.Columns(e.ColumnIndex).Name()
                    Case "IDPRODUCTO"
                        If COM_DETALLEORDENCOMPRADataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value IsNot DBNull.Value Then _
                            Me.txtDescripProducto.Text = daProductos.GetNombreProducto(COM_DETALLEORDENCOMPRADataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
                        ActualizaDatosProd(e.RowIndex)
                        SalvaCambios(False)
                    Case "CANTIDAD"
                        SalvaCambios(False)
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub COM_DETALLEORDENCOMPRADataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles COM_DETALLEORDENCOMPRADataGridView.CellValidating
        Try
            If COM_DETALLEORDENCOMPRADataGridView.IsCurrentCellInEditMode AndAlso e.FormattedValue IsNot Nothing AndAlso e.FormattedValue <> "" Then
                Select Case COM_DETALLEORDENCOMPRADataGridView.Columns(e.ColumnIndex).Name()
                    Case "IDPRODUCTO"
                        If e.FormattedValue IsNot Nothing AndAlso e.FormattedValue IsNot DBNull.Value AndAlso e.FormattedValue <> "" Then
                            Dim NomProd As Object = daProductos.GetNombreProducto(e.FormattedValue)
                            If NomProd Is Nothing Then
                                MessageBox.Show("Producto con este codigo no existe, verifique e intente de nuevo", "No existe", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                e.Cancel = True
                            End If
                        End If
                    Case "CANTIDAD"
                        If e.FormattedValue < 0 Then
                            e.Cancel = True
                            MessageBox.Show("La cantidad debe ser mayor de cero", "Cantidad", MessageBoxButtons.OK)
                        End If

                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    Private Sub COM_DETALLEORDENCOMPRADataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles COM_DETALLEORDENCOMPRADataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub

    Private Sub COM_DETALLEORDENCOMPRADataGridView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles COM_DETALLEORDENCOMPRADataGridView.GotFocus

        Dim cVal As New cValidaciones(DsCompras)
        Try
            If TIPODOCTextBox.Text.Length > 0 And NUMEROTextBox.Text.Length > 0 Then
                COM_ENCABEZADOORDENCOMPRABindingSource.EndEdit()
                If cVal IsNot Nothing Then
                    mensajeValidacion = cVal.ValidaOrdenCompra(iTipoDoc.DocEs)
                    If mensajeValidacion <> "" Then
                        SUCURSALGLOBALCOMTextBox.Focus()
                        MessageBox.Show(mensajeValidacion, "validaciones", MessageBoxButtons.OK)
                    Else
                        If DsCompras.COM_ENCABEZADOORDENCOMPRA.Rows(COM_ENCABEZADOORDENCOMPRABindingSource.Position).RowState = DataRowState.Added Then
                            NUMEROTextBox.Text = lbGlobalcom.Consec.GetNumeroActual(TIPODOCTextBox.Text, iTipoDoc.DocEs)
                            SalvaCambios(False)
                        End If
                    End If
                End If
            Else
                SUCURSALGLOBALCOMTextBox.Focus()
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try

        Try
            If NUMEROTextBox.Text.Length > 0 AndAlso TIPODOCTextBox.Text.Length > 0 Then
                COM_ENCABEZADOORDENCOMPRABindingSource.EndEdit()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub COM_DETALLEORDENCOMPRADataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles COM_DETALLEORDENCOMPRADataGridView.KeyDown
        Try
            If e.Control Then
                Select Case e.KeyCode
                    Case Keys.B
                        If TIPODOCTextBox.Text <> "" And NUMEROTextBox.Text <> "" Then
                            If COM_DETALLEORDENCOMPRADataGridView.CurrentCellAddress.X = 3 Then
                                Dim fLProd As New fListaProductos
                                fLProd.ListaPrecios = daTipodoc.GetIdLista(TIPODOCTextBox.Text)
                                If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                                    COM_DETALLEORDENCOMPRADataGridView.BeginEdit(True)
                                    COM_DETALLEORDENCOMPRADataGridView.Rows(COM_DETALLEORDENCOMPRADataGridView.CurrentRow.Index).Cells(3).Value = fLProd.idArticulo
                                    COM_DETALLEORDENCOMPRADataGridView.EndEdit()
                                    Me.txtDescripProducto.Text = daProductos.GetNombreProducto(fLProd.idArticulo)
                                    pCodigoProducto = fLProd.idArticulo
                                    addProductos(pCodigoProducto)
                                    ActualizaDatosProd(COM_DETALLEORDENCOMPRADataGridView.CurrentCellAddress.Y)
                                End If
                            End If
                        End If
                    Case Keys.S
                        cOrdCom = New lbGlobalcom.cEncabezadoOrdenCompra(TIPODOCTextBox.Text, NUMEROTextBox.Text, "COM")
                        If cOrdCom.validarNumero(NUMEROTextBox.Text) = 0 Then
                            NUMEROTextBox.Text = cOrdCom.consecutivoOrdenCompra()
                        End If
                        daOrdenCompraEncabeza.Update(DsCompras.COM_ENCABEZADOORDENCOMPRA)
                        COM_DETALLEORDENCOMPRABindingSource.EndEdit()
                        daOrdenCompraDtalle.Update(DsCompras.COM_DETALLEORDENCOMPRA)
                        MessageBox.Show("Se salvarón registros con exito", "Salva Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub TotalizaDoc()
        Try
            Dim TotalBase As Double = 0
            Dim TotalIva As Double = 0
            Dim Totaldoc As Double = 0
            For Each drDetMov As dsCompras.COM_DETALLEORDENCOMPRARow In DsCompras.COM_DETALLEORDENCOMPRA.Rows
                If drDetMov.RowState <> DataRowState.Deleted Then
                    TotalBase += drDetMov.PRECIOBASE * drDetMov.CANTIDAD
                    TotalIva += drDetMov.PRECIOIVA * drDetMov.CANTIDAD
                End If
            Next
            Me.TXTTOTALBASE.Text = TotalBase.ToString("c2")
            Me.TXTTOTALIVA.Text = TotalIva.ToString("C2")
            Me.TXTTOTALDOC.Text = (TotalBase + TotalIva).ToString("c2")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub COM_DETALLEORDENCOMPRADataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles COM_DETALLEORDENCOMPRADataGridView.RowEnter
        Try
            TotalizaDoc()
            If e.RowIndex >= 0 AndAlso COM_DETALLEORDENCOMPRADataGridView.Rows(e.RowIndex).Cells(3).Value IsNot Nothing AndAlso COM_DETALLEORDENCOMPRADataGridView.Rows(e.RowIndex).Cells(3).Value IsNot DBNull.Value Then
                Me.txtDescripProducto.Text = daProductos.GetNombreProducto(COM_DETALLEORDENCOMPRADataGridView.Rows(e.RowIndex).Cells(2).Value)
            Else
                Me.txtDescripProducto.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try
            If ESTADOORDENListBox.SelectedValue <> 3 Then
                If MessageBox.Show("Esta seguro que desea eliminar este documento", "Elimina Documento", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    COM_ENCABEZADOORDENCOMPRABindingSource.RemoveCurrent()
                    lEliminaDoc = True
                    SalvaCambios(False)
                    MessageBox.Show("Se elimino documento con exito", "Elimino Documento", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
                End If
            Else
                MessageBox.Show("No se puede eliminar el documento ya fue cargado", "Elimina Documento", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    
    Private Sub btnEliminarReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarReg.Click
        Try
            If COM_DETALLEORDENCOMPRADataGridView.CurrentRow IsNot Nothing Then
                COM_DETALLEORDENCOMPRABindingSource.RemoveCurrent()
                lEliminaDoc = True
                SalvaCambios(False)
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class